package com.example.webmagic.csdn;

import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by Geekkiller on 2017/11/16.
 */
public class CsdnBlogDao {

    private static Connection getConn() {
        Connection connection = null;
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/data_repository?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";
        try {
            Class.forName(driver); // classLoader,加载对应驱动
            connection = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public int add(CsdnBlog csdnBlog) {
        try {
            String sql = "INSERT INTO `csdnblog` (`keyes`, `titles`, `content` , `dates`, `tags`, `category`, `views`, `comments`, `copyright`) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?);";
            PreparedStatement ps = CsdnBlogDao.getConn().prepareStatement(sql);
            ps.setInt(1, csdnBlog.getKey());
            ps.setString(2, csdnBlog.getTitle());
            ps.setString(3, csdnBlog.getContent());
            ps.setString(4, csdnBlog.getDate());
            ps.setString(5, csdnBlog.getTags());
            ps.setString(6, csdnBlog.getCategory());
            ps.setInt(7, csdnBlog.getView());
            ps.setInt(8, csdnBlog.getComments());
            ps.setInt(9, csdnBlog.getCopyright());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


}
